<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\PackageEase;

/**
 * PackageEaseSearch represents the model behind the search form about `backend\models\PackageEase`.
 */
class PackageEaseSearch extends PackageEase
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'province', 'city', 'principal_range', 'is_migrated'], 'integer'],
            [['code', 'title', 'creditor', 'principal', 'interest', 'principal_interest', 'hushu', 'price', 'hushu_suit', 'principle_suit', 'percent_principle_suit',
                'origin', 'discount', 'total_mortgage_principal', 'owner', 'created_at', 'level', 'other_note', 'highlights', 'follow', 'published_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = PackageEase::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ],
            ]
        ]);

//        $query->joinWith(['publisher' => function ($query) {
//            $query->from(['publisher' => 'backend_users']);
//        }]);

        $dataProvider->sort->attributes['owner'] = [
            'asc' => ['owner' => SORT_ASC],
            'desc' => ['owner' => SORT_DESC],
        ];

//        if (Yii::$app->user->identity->superadmin or in_array(Yii::$app->user->identity->realname, Yii::$app->params['special_access_users'])) {
////        if (in_array(Yii::$app->user->identity->realname, Yii::$app->params['special_access_users'])) {
//
//        } else {
//            $same_department = BackendUsers::find()->where(['department' => Yii::$app->user->identity->department])->select('id')->indexBy('id')->asArray()->all();
//            $query->andWhere(['in', 'owner', array_keys($same_department)]);
//        }

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'province' => $this->province,
            'principal_range' => $this->principal_range,
            'created_at' => $this->created_at,
            'level' => $this->level,
            'owner' => $this->owner,

//            'highlights', 'follow',
            'published_at' => $this->published_at,
            'updated_at' => $this->updated_at,
            'is_migrated' => $this->is_migrated,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'creditor', $this->creditor])
            ->andFilterWhere(['like', 'principal', $this->principal])
            ->andFilterWhere(['like', 'interest', $this->interest])
            ->andFilterWhere(['like', 'principal_interest', $this->principal_interest])
            ->andFilterWhere(['like', 'hushu', $this->hushu])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'hushu_suit', $this->hushu_suit])
            ->andFilterWhere(['like', 'principle_suit', $this->principle_suit])
            ->andFilterWhere(['like', 'percent_principle_suit', $this->percent_principle_suit])
            ->andFilterWhere(['like', 'origin', $this->origin])
            ->andFilterWhere(['like', 'discount', $this->discount])
            ->andFilterWhere(['like', 'total_mortgage_principal', $this->total_mortgage_principal])
            ->andFilterWhere(['like', 'owner', $this->owner])
            ->andFilterWhere(['like', 'hightlights', $this->highlights])
            ->andFilterWhere(['like', 'hightlights', $this->follow])
            ->andFilterWhere(['like', 'hightlights', $this->other_note]);


        if ($this->city) {
            $query->andWhere("FIND_IN_SET($this->city,`city`)");
        }

        return $dataProvider;
    }
}
